/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JIFChamadoTecnico.java
 *
 * Created on 11/02/2012, 00:03:32
 */
package view.tecnico;

import control.ChamadoControl;
import control.Sessao;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import model.Chamado;
import model.Pessoa;
import model.Resposta;
import util.CellRenderer;
import view.IJIFSimpleFactory;
import view.admin.JDFinalizarAdmin;
import view.admin.JDResponderEAAdmin;
import view.admin.JDVisualizarChamAtr;
import view.admin.JIFChamadoAdmin;

/**
 *
 * @author Felipe
 */
public class JIFChamadoTecnico extends JInternalFrame implements IJIFSimpleFactory {
    
    private Pessoa usuarioLogado;
    private ChamadoControl controller;

    /** Creates new form JIFChamadoTecnico */
    public JIFChamadoTecnico() {
        this.usuarioLogado = Sessao.getInstance().getUsuarioLogado();
        // pobrema detected
        controller = ChamadoControl.getControl();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jTabbedPane1 = new javax.swing.JTabbedPane();
        pnlEA = new javax.swing.JPanel();
        pnlEASuperior = new javax.swing.JPanel();
        pnlBusca = new javax.swing.JPanel();
        cbbBusca = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        txtBusca = new javax.swing.JTextField();
        pnlJtb = new javax.swing.JPanel();
        jtbSuperior = new javax.swing.JToolBar();
        btnVisualizarEA = new javax.swing.JButton();
        btnResponderEA = new javax.swing.JButton();
        btnIniciarIntervencao = new javax.swing.JButton();
        btnEncerrarIntervencao = new javax.swing.JButton();
        btnEncerrarEA = new javax.swing.JButton();
        pnlSuperiorCentro = new javax.swing.JPanel();
        pnlSupCenDiv = new javax.swing.JPanel();
        pnlEAOeste = new javax.swing.JPanel();
        pnlEALeste = new javax.swing.JPanel();
        pnlEASul = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEA = new javax.swing.JTable();
        pnlFin = new javax.swing.JPanel();
        pnlFinSuperior = new javax.swing.JPanel();
        pnlFinBusca = new javax.swing.JPanel();
        cbbFinBusca = new javax.swing.JComboBox();
        btnFinBuscar = new javax.swing.JButton();
        txtFinBusca = new javax.swing.JTextField();
        pnlFinJtb = new javax.swing.JPanel();
        jtbFinSup = new javax.swing.JToolBar();
        btnVisualizarEncerrado = new javax.swing.JButton();
        pnlFinSupCen = new javax.swing.JPanel();
        pnlFinSupCenDiv = new javax.swing.JPanel();
        pnlFinLeste = new javax.swing.JPanel();
        pnlFinOeste = new javax.swing.JPanel();
        pnlFinSul = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblEncerrados = new javax.swing.JTable();

        setClosable(true);
        setTitle("Controle de chamado");
        setPreferredSize(new java.awt.Dimension(745, 586));

        pnlEA.setLayout(new java.awt.BorderLayout());

        pnlEASuperior.setMinimumSize(new java.awt.Dimension(364, 50));
        pnlEASuperior.setPreferredSize(new java.awt.Dimension(613, 50));
        pnlEASuperior.setLayout(new java.awt.BorderLayout());

        pnlBusca.setMaximumSize(new java.awt.Dimension(430, 20));
        pnlBusca.setMinimumSize(new java.awt.Dimension(430, 20));
        pnlBusca.setPreferredSize(new java.awt.Dimension(430, 20));
        pnlBusca.setLayout(new javax.swing.BoxLayout(pnlBusca, javax.swing.BoxLayout.LINE_AXIS));

        cbbBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        cbbBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlBusca.add(cbbBusca);

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/buscar2.png"))); // NOI18N
        btnBuscar.setToolTipText("Procurar");
        btnBuscar.setMaximumSize(new java.awt.Dimension(30, 20));
        btnBuscar.setMinimumSize(new java.awt.Dimension(30, 20));
        btnBuscar.setOpaque(false);
        btnBuscar.setPreferredSize(new java.awt.Dimension(30, 20));
        pnlBusca.add(btnBuscar);

        txtBusca.setMargin(new java.awt.Insets(200, 20, 20, 20));
        txtBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        txtBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlBusca.add(txtBusca);

        pnlEASuperior.add(pnlBusca, java.awt.BorderLayout.EAST);

        pnlJtb.setLayout(new java.awt.BorderLayout());

        jtbSuperior.setBorder(null);
        jtbSuperior.setFloatable(false);
        jtbSuperior.setRollover(true);
        jtbSuperior.setOpaque(false);

        btnVisualizarEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/view1.gif"))); // NOI18N
        btnVisualizarEA.setToolTipText("Visualizar chamado");
        btnVisualizarEA.setFocusable(false);
        btnVisualizarEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnVisualizarEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnVisualizarEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnVisualizarEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarEAActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnVisualizarEA);

        btnResponderEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/responder1.png"))); // NOI18N
        btnResponderEA.setToolTipText("Responder chamado em aberto");
        btnResponderEA.setFocusable(false);
        btnResponderEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnResponderEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnResponderEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnResponderEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResponderEAActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnResponderEA);

        btnIniciarIntervencao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/relogio.png"))); // NOI18N
        btnIniciarIntervencao.setToolTipText("Iniciar intervenção técnica");
        btnIniciarIntervencao.setFocusable(false);
        btnIniciarIntervencao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnIniciarIntervencao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnIniciarIntervencao, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnIniciarIntervencao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarIntervencaoActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnIniciarIntervencao);

        btnEncerrarIntervencao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/fim5.png"))); // NOI18N
        btnEncerrarIntervencao.setToolTipText("Encerrar intervenção técnica");
        btnEncerrarIntervencao.setFocusable(false);
        btnEncerrarIntervencao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEncerrarIntervencao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnEncerrarIntervencao, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnEncerrarIntervencao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncerrarIntervencaoActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnEncerrarIntervencao);

        btnEncerrarEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/joia.png"))); // NOI18N
        btnEncerrarEA.setToolTipText("Finalizar chamado");
        btnEncerrarEA.setFocusable(false);
        btnEncerrarEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEncerrarEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnEncerrarEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnEncerrarEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncerrarEAActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnEncerrarEA);

        pnlJtb.add(jtbSuperior, java.awt.BorderLayout.CENTER);

        pnlEASuperior.add(pnlJtb, java.awt.BorderLayout.WEST);

        pnlSuperiorCentro.setLayout(new javax.swing.BoxLayout(pnlSuperiorCentro, javax.swing.BoxLayout.LINE_AXIS));

        pnlSupCenDiv.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlSupCenDiv.setMaximumSize(new java.awt.Dimension(1000, 20));
        pnlSupCenDiv.setOpaque(false);
        pnlSupCenDiv.setPreferredSize(new java.awt.Dimension(1000, 20));
        pnlSuperiorCentro.add(pnlSupCenDiv);

        pnlEASuperior.add(pnlSuperiorCentro, java.awt.BorderLayout.CENTER);

        pnlEA.add(pnlEASuperior, java.awt.BorderLayout.PAGE_START);
        pnlEA.add(pnlEAOeste, java.awt.BorderLayout.WEST);
        pnlEA.add(pnlEALeste, java.awt.BorderLayout.EAST);
        pnlEA.add(pnlEASul, java.awt.BorderLayout.SOUTH);

        tblEA.setModel(controller.getChamadosEATableModel());
        tblEA.setRowHeight(20);
        ajustarColunasEmAndamento();
        jScrollPane1.setViewportView(tblEA);

        pnlEA.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Em aberto", pnlEA);

        pnlFin.setLayout(new java.awt.BorderLayout());

        pnlFinSuperior.setMinimumSize(new java.awt.Dimension(364, 50));
        pnlFinSuperior.setPreferredSize(new java.awt.Dimension(613, 50));
        pnlFinSuperior.setLayout(new java.awt.BorderLayout());

        pnlFinBusca.setMaximumSize(new java.awt.Dimension(430, 20));
        pnlFinBusca.setMinimumSize(new java.awt.Dimension(430, 20));
        pnlFinBusca.setPreferredSize(new java.awt.Dimension(430, 20));
        pnlFinBusca.setLayout(new javax.swing.BoxLayout(pnlFinBusca, javax.swing.BoxLayout.LINE_AXIS));

        cbbFinBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbFinBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbFinBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        cbbFinBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlFinBusca.add(cbbFinBusca);

        btnFinBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/buscar2.png"))); // NOI18N
        btnFinBuscar.setToolTipText("Procurar");
        btnFinBuscar.setMaximumSize(new java.awt.Dimension(30, 20));
        btnFinBuscar.setMinimumSize(new java.awt.Dimension(30, 20));
        btnFinBuscar.setOpaque(false);
        btnFinBuscar.setPreferredSize(new java.awt.Dimension(30, 20));
        pnlFinBusca.add(btnFinBuscar);

        txtFinBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        txtFinBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        txtFinBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlFinBusca.add(txtFinBusca);

        pnlFinSuperior.add(pnlFinBusca, java.awt.BorderLayout.EAST);

        pnlFinJtb.setLayout(new java.awt.BorderLayout());

        jtbFinSup.setBorder(null);
        jtbFinSup.setFloatable(false);
        jtbFinSup.setRollover(true);
        jtbFinSup.setOpaque(false);

        btnVisualizarEncerrado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/view1.gif"))); // NOI18N
        btnVisualizarEncerrado.setToolTipText("Visualizar chamado encerrado");
        btnVisualizarEncerrado.setFocusable(false);
        btnVisualizarEncerrado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnVisualizarEncerrado.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbFinSup, org.jdesktop.beansbinding.ELProperty.create("false"), btnVisualizarEncerrado, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnVisualizarEncerrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarEncerradoActionPerformed(evt);
            }
        });
        jtbFinSup.add(btnVisualizarEncerrado);

        pnlFinJtb.add(jtbFinSup, java.awt.BorderLayout.CENTER);

        pnlFinSuperior.add(pnlFinJtb, java.awt.BorderLayout.WEST);

        pnlFinSupCen.setLayout(new javax.swing.BoxLayout(pnlFinSupCen, javax.swing.BoxLayout.LINE_AXIS));

        pnlFinSupCenDiv.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlFinSupCenDiv.setMaximumSize(new java.awt.Dimension(1000, 20));
        pnlFinSupCenDiv.setOpaque(false);
        pnlFinSupCenDiv.setPreferredSize(new java.awt.Dimension(1000, 20));
        pnlFinSupCen.add(pnlFinSupCenDiv);

        pnlFinSuperior.add(pnlFinSupCen, java.awt.BorderLayout.CENTER);

        pnlFin.add(pnlFinSuperior, java.awt.BorderLayout.PAGE_START);
        pnlFin.add(pnlFinLeste, java.awt.BorderLayout.EAST);
        pnlFin.add(pnlFinOeste, java.awt.BorderLayout.WEST);
        pnlFin.add(pnlFinSul, java.awt.BorderLayout.SOUTH);

        tblEncerrados.setModel(controller.getChamadosEncerradosTableModel());
        tblEncerrados.setRowHeight(20);
        ajustarColunasEncerrados();
        jScrollPane2.setViewportView(tblEncerrados);

        pnlFin.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Encerrados", pnlFin);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVisualizarEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarEAActionPerformed
        visualizarChamadoEA();
    }//GEN-LAST:event_btnVisualizarEAActionPerformed

    private void btnResponderEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResponderEAActionPerformed
        responderChamadoEA();
    }//GEN-LAST:event_btnResponderEAActionPerformed

    private void btnIniciarIntervencaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarIntervencaoActionPerformed
        int ret = JOptionPane.showConfirmDialog(null, 
                "Deseja iniciar uma intervenção para este chamado?", 
                "Intervenção", JOptionPane.YES_NO_OPTION);
        if (ret == JOptionPane.YES_OPTION) {
            iniciarIntervencao();
        }
    }//GEN-LAST:event_btnIniciarIntervencaoActionPerformed

    private void btnEncerrarIntervencaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncerrarIntervencaoActionPerformed
        finalizarIntervencao();
    }//GEN-LAST:event_btnEncerrarIntervencaoActionPerformed

    private void btnEncerrarEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncerrarEAActionPerformed
        encerrarChamadoEA();
    }//GEN-LAST:event_btnEncerrarEAActionPerformed

    private void btnVisualizarEncerradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarEncerradoActionPerformed
        visualizarChamadoEnce();
    }//GEN-LAST:event_btnVisualizarEncerradoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEncerrarEA;
    private javax.swing.JButton btnEncerrarIntervencao;
    private javax.swing.JButton btnFinBuscar;
    private javax.swing.JButton btnIniciarIntervencao;
    private javax.swing.JButton btnResponderEA;
    private javax.swing.JButton btnVisualizarEA;
    private javax.swing.JButton btnVisualizarEncerrado;
    private javax.swing.JComboBox cbbBusca;
    private javax.swing.JComboBox cbbFinBusca;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jtbFinSup;
    private javax.swing.JToolBar jtbSuperior;
    private javax.swing.JPanel pnlBusca;
    private javax.swing.JPanel pnlEA;
    private javax.swing.JPanel pnlEALeste;
    private javax.swing.JPanel pnlEAOeste;
    private javax.swing.JPanel pnlEASul;
    private javax.swing.JPanel pnlEASuperior;
    private javax.swing.JPanel pnlFin;
    private javax.swing.JPanel pnlFinBusca;
    private javax.swing.JPanel pnlFinJtb;
    private javax.swing.JPanel pnlFinLeste;
    private javax.swing.JPanel pnlFinOeste;
    private javax.swing.JPanel pnlFinSul;
    private javax.swing.JPanel pnlFinSupCen;
    private javax.swing.JPanel pnlFinSupCenDiv;
    private javax.swing.JPanel pnlFinSuperior;
    private javax.swing.JPanel pnlJtb;
    private javax.swing.JPanel pnlSupCenDiv;
    private javax.swing.JPanel pnlSuperiorCentro;
    private javax.swing.JTable tblEA;
    private javax.swing.JTable tblEncerrados;
    private javax.swing.JTextField txtBusca;
    private javax.swing.JTextField txtFinBusca;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void visualizarChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        JDVisualizarChamAtr visualizarChamado = new JDVisualizarChamAtr(null, 
                                true, false, chamado, usuarioLogado);
    }
    
    private void responderChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        boolean responder = false;
                
        if (!chamado.getRespostas().isEmpty()) {
            if (chamado.getRespostas().get(chamado.getRespostas().size()-1).isAgindo()) {
                responder = false;
            } else {
                responder = true;                
            }
        } else {
            responder = true;            
        }
        
        if (responder == true) {
            JDResponderEAAdmin jdResponderEAAdmin = new JDResponderEAAdmin(null, false, 
                    false, chamado, usuarioLogado);
        } else {
            JOptionPane.showMessageDialog(null, "Este chamado está em intervenção técnica!");
        }
        
    }   
    
     private void iniciarIntervencao() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        boolean iniciarIntervencao = false;
        
        if (chamado.getRespostas().isEmpty()) {
            iniciarIntervencao = true;            
        } 
        
        else {    
            Resposta resposta = chamado.getRespostas().get(chamado.getRespostas().size()-1);  
            if (!resposta.isAgindo()) {
                iniciarIntervencao = true;
            } else {
                JOptionPane.showMessageDialog(null, "Este chamado está em intervenção técnica!");
            }
        }
        
        if (iniciarIntervencao == true) {
            try {                
                controller.iniciarIntervencao(chamado, getLinha(tblEA), usuarioLogado);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Um erro ocorreu! +\n" + ex.getMessage());
                Logger.getLogger(JIFChamadoAdmin.class.getName()).log(Level.SEVERE, null, ex);
            }        
        }        
    }
     
     private void finalizarIntervencao() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
                
        if (!chamado.getRespostas().isEmpty()) {
            Resposta resposta = chamado.getRespostas().get(chamado.getRespostas().size()-1);  
            if (resposta.isAgindo()) {
                JDResponderEAAdmin jdResponderEAAdmin = new JDResponderEAAdmin(null, false, 
                                true, chamado, usuarioLogado, resposta, getLinha(tblEA));
            } else {
                JOptionPane.showMessageDialog(null, "Este chamado não possui nenhuma intervenção em aberto.");
            }         
        } else {
            JOptionPane.showMessageDialog(null, "Este chamado não possui nenhuma intervenção em aberto.");
        } 
    }
    
     private void encerrarChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
                
        boolean encerrar = false;
        
        if (chamado.getRespostas() == null) {
            ArrayList<Resposta> respostas = new ArrayList<Resposta>(); 
            chamado.setRespostas(respostas);
        }
        
        if (!chamado.getRespostas().isEmpty()) {
            Resposta resposta = chamado.getRespostas().get(chamado.getRespostas().size()-1);  
            if (resposta.isAgindo()) {
                JOptionPane.showMessageDialog(null, "Este chamado está em intervenção técnica e não pode ser encerrado.");
            } else {
                encerrar = true;
            }         
        } else {
            encerrar = true;
        } 
        
        if (encerrar == true) {
            JDFinalizarAdmin finalizar = new JDFinalizarAdmin(null, true, chamado, true, usuarioLogado);
        }
        
    }
     
    private void visualizarChamadoEnce() {
        Chamado chamado = controller.getChamadosEncerradosTableModel().getChamadoEJB(getLinha(tblEncerrados));
        JDVisualizarChamAtr visualizarChamado = new JDVisualizarChamAtr(null, true, true, chamado, usuarioLogado);
    }
      
    private int getLinha(JTable tbl) {
        return tbl.convertRowIndexToModel(tbl.getSelectedRow());
    }   
    
    private void ajustarColunasEmAndamento() {     
        TableCellRenderer tcr = new CellRenderer();  
                
        tblEA.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblEA.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tblEA.getColumnModel().getColumn(2).setCellRenderer(tcr);
        
        tblEA.getColumnModel().getColumn(0).setMaxWidth(70);
        tblEA.getColumnModel().getColumn(1).setMaxWidth(120);
        tblEA.getColumnModel().getColumn(1).setMinWidth(150);
        tblEA.getColumnModel().getColumn(2).setMinWidth(120);
        tblEA.getColumnModel().getColumn(2).setMaxWidth(150);
        tblEA.getColumnModel().getColumn(3).setMinWidth(180);
        tblEA.getColumnModel().getColumn(3).setMaxWidth(170);
        tblEA.getColumnModel().getColumn(4).setMinWidth(180);
        tblEA.getColumnModel().getColumn(4).setMaxWidth(170);    
    }
    
    private void ajustarColunasEncerrados() {     
        TableCellRenderer tcr = new CellRenderer();  
        
        tblEncerrados.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblEncerrados.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tblEncerrados.getColumnModel().getColumn(2).setCellRenderer(tcr);
        
        tblEncerrados.getColumnModel().getColumn(0).setMaxWidth(70);
        tblEncerrados.getColumnModel().getColumn(1).setMaxWidth(80);
        tblEncerrados.getColumnModel().getColumn(1).setMinWidth(150);
        tblEncerrados.getColumnModel().getColumn(2).setMinWidth(90);
        tblEncerrados.getColumnModel().getColumn(2).setMaxWidth(150);
        tblEncerrados.getColumnModel().getColumn(3).setMinWidth(180);
        tblEncerrados.getColumnModel().getColumn(3).setMaxWidth(170);
        tblEncerrados.getColumnModel().getColumn(4).setMinWidth(180);
        tblEncerrados.getColumnModel().getColumn(4).setMaxWidth(170);    
    }
    
   
}
